package com.atguigu.day19.steam;

import org.junit.Test;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
流式编程: 目的写出简洁高效的代码
  创建:
      集合
      数组
      Stream.of()有限
      Stream.generate()无限
  中间
     去重
     过滤
     排序
     限制数量
     跳过指定数量
     将流中的每个值都换成另一个流，然后把所有流连接成一个流
  终结: 只能有一个
     迭代
     匹配
     数量
     查找
     获取最值
     将流中的值进行合并
     收集数据
 */
public class EndTest {

    @Test
    public void test08(){
        //数据的收集
        List<Integer> list = Stream.of(11, 44, 33, 22, 77, 66, 55, 44)
                .distinct()
                .sorted()
                .limit(3)
                .toList();
        System.out.println("list = " + list);
    }

    @Test
    public void test07(){
        //流中数据合并 2
        Integer reduce = Stream.of(1, 2, 3)/*.reduce(5, new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return Integer.sum(integer, integer2);
            }
        });*/
                .reduce(5,Integer::sum);
        System.out.println("reduce = " + reduce);

    }

    @Test
    public void test06(){
        //流中数据合并 1
        Optional<Integer> reduce = Stream.of(1, 2, 3)
                /*.reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return Integer.sum(integer,integer2);
            }
        });*/
                        .reduce(Integer::sum);
        System.out.println("reduce = " + reduce);

    }

    @Test
    public void test05(){
        //获取最值
        Optional<Integer> max = Stream.of(11, 44, 33, 22, 77, 66, 55, 44)
                /*.max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        });*/
                        .max(Integer::compare);
        System.out.println("max = " + max);
    }

    @Test
    public void test04(){
        //查找
        Optional<Integer> first = Stream.of(11, 44, 33, 22, 77, 66, 55, 44).findFirst();
        System.out.println("first = " + first);
    }

    @Test
    public void test03(){
        //count 数量

        long count = Stream.of(11, 44, 33, 22, 77, 66, 55, 44)
                .distinct()

                .filter(i->i%2!=0)
                .peek(System.out::println)


                .count();

        System.out.println("count = " + count);

    }


    @Test
    public void test02(){
        boolean match = Stream.of(11, 44, 33, 22, 77, 66, 55, 44)

              /*  .allMatch(new Predicate<Integer>() {
                    @Override
                    public boolean test(Integer integer) {
                        return integer%2==0;
                    }
                });*/
                      .anyMatch(i->i%2==0);//匹配任意一个

        System.out.println("match = " + match);

    }
    @Test
    public void test01(){
        //迭代

        Stream.of(11,44,33,22,77,66,55,44)
                .forEach(System.out::println);

    }
}
